package com.example.month11.indicator;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

/**
 * Indicator容器类,包含itemView和底部跟踪指示器
 */
public class IndicatorGroupView extends FrameLayout {
    // 2.动态添加View的容器- 指示器item的容器
    private LinearLayout mIndicatorGroup;
    private View mBottomTrackView;
    private int mItemWidth; // 条目宽度
    private LayoutParams mTrackParams; // 底部指示器的layoutParams
    private int mInitLeftMargin; // 初始的距离左边距离。

    public IndicatorGroupView(Context context) {
        this(context, null);
    }

    public IndicatorGroupView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndicatorGroupView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 3.初始化指示器条目的容器
        mIndicatorGroup = new LinearLayout(context);
        addView(mIndicatorGroup); // 只能添加一个子容器
        // 让底部view的宽度，占据一个item的宽度(指定宽度、)。
    }

    /**
     * 添加itemView
     *
     * @param itemView
     */
    public void addItemView(View itemView) {
        mIndicatorGroup.addView(itemView);
    }

    /**
     * 获取当前位置的item
     *
     * @param position
     * @return
     */
    public View getItemAt(int position) {
        return mIndicatorGroup.getChildAt(position);
    }

    // 添加底部跟踪的指示器
    public void addBottomTrackView(View bottomTrackView, int itemWidth) {
        if (bottomTrackView == null) {
            return;
        }
        this.mItemWidth = itemWidth;
        this.mBottomTrackView = bottomTrackView;
        // 添加底部跟踪指示器view
        addView(mBottomTrackView);
        // 指示器。显示到底部。
        mTrackParams = (LayoutParams) mBottomTrackView.getLayoutParams();
        mTrackParams.gravity = Gravity.BOTTOM;

        // 宽度：如果用户设置列，就用设置的值如88，否则就用mItemWidth
        int trackWidth = mTrackParams.width;
        if (mTrackParams.width == ViewGroup.LayoutParams.MATCH_PARENT) {
            // 未设置宽度
            trackWidth = mItemWidth;
        }
        if (trackWidth > mItemWidth) { // 设置的宽度过大
            trackWidth = mItemWidth;
        }
        // 最后确定指示器宽度。
        mTrackParams.width = trackWidth;
        // 确保指示器在中间显示
        mInitLeftMargin = (mItemWidth - trackWidth) / 2;
        mTrackParams.leftMargin = mInitLeftMargin;
    }

    // 滚动底部指示器: leftMargin
    public void scrollBottomTrack(int position, float positionOffset) {
        if (mBottomTrackView == null) return;

        int leftMargin = (int) (position + positionOffset) * mItemWidth;
        mTrackParams.leftMargin = leftMargin + mInitLeftMargin;
        mBottomTrackView.setLayoutParams(mTrackParams);
    }

    /**
     * 点击，移动底部指示器，带动画效果：leftMargin
     *
     * @param position
     */
    public void scrollBottomTrack(int position) {
        if (mBottomTrackView == null) return;

        // 最终要移动的位置
        int finalLeftMargin = (int) (position) * mItemWidth + mInitLeftMargin;
        //当前的位置
        int currentLeftMargin = mTrackParams.leftMargin;
        // 移动的距离
        int distance = finalLeftMargin - currentLeftMargin;

        ValueAnimator animator = ObjectAnimator.ofFloat(currentLeftMargin, finalLeftMargin)
                .setDuration(Math.abs(distance) / 2);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // 不断回调方法，不断地设置leftMargin
                float currentLeftMargin = (float) animation.getAnimatedValue();
                mTrackParams.leftMargin = (int) currentLeftMargin;
                mBottomTrackView.setLayoutParams(mTrackParams);
            }
        });
        animator.setInterpolator(new DecelerateInterpolator());
        animator.start();
    }
}
